<template>
  <div class="my-conainer">
    <!-- 已登陆 -->
    <div v-if="user" class="header yes-login">
      <div class="top">
        <div class="left">
          <van-image class="tu" round fit="cover" :src="userinfo.photo" />
          <span>{{ userinfo.name }}</span>
        </div>
        <div class="right">
          <van-button round size="mini" to="/user/profile">编辑资料</van-button>
        </div>
      </div>
      <div class="bottom">
        <div class="data-item">
          <span class="num">{{ userinfo.art_count }}</span>
          <span class="texts">头条</span>
        </div>
        <div class="data-item">
          <span class="num">{{ userinfo.follow_count }}</span>
          <span class="texts">关注</span>
        </div>
        <div class="data-item">
          <span class="num">{{ userinfo.fans_count }}</span>
          <span class="texts">粉丝</span>
        </div>
        <div class="data-item">
          <span class="num">{{ userinfo.like_count }}</span>
          <span class="texts">获赞</span>
        </div>
      </div>
    </div>

    <!-- 未登录 -->
    <div v-else class="header not-login">
      <div class="ce" @click="$router.push('/login')">
        <img src="~@/assets/mobile.png" />
        <span>登录 / 注册</span>
      </div>
    </div>

    <!-- 导航 -->
    <van-grid class="grid-dh" :column-num="2" clickable>
      <van-grid-item class="grid-item">
        <i slot="icon" class="su su-shoucang"></i>
        <span slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <i slot="icon" class="su su-lishi"></i>
        <span slot="text">历史</span>
      </van-grid-item>
    </van-grid>

    <!-- 单元格 -->
    <van-cell title="消息通知" is-link />
    <van-cell class="distance" title="小智同学" is-link />
    <van-button @click="exit" v-if="user" type="primary" color="#fff" block>
      <span class="exit" slot="default">退出登录</span>
    </van-button>
  </div>
</template>

<script>
import { mapState } from "vuex";
// 将获取自己信息的接口引入进来
import { openUser } from "@/api/user";

export default {
  // 组件名称
  name: "",
  // 组件参数 接收来自父组件的数据
  props: {},
  // 局部注册的组件
  components: {},
  // 组件状态值
  data() {
    return {
      // 里边存储自己的个人信息
      userinfo: {},
    };
  },
  // 计算属性
  computed: {
    ...mapState(["user"]),
  },
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    exit() {
      this.$dialog
        .confirm({
          title: "是否确认退出?",
        })
        .then(() => {
          // 确认退出后清除token
          this.$store.commit("setUser", null);
        })
        .catch(() => {
          // 取消
        });
    },
    // 获取自己信息并将信息保存到userinfo对象中
    async users() {
      try {
        const { data } = await openUser();
        this.userinfo = data.data;
      } catch (error) {
        this.$toast("用户信息获取失败，请稍后再试!");
      }
    },
  },
  // 以下是生命周期钩子   注：没用到的钩子请自行删除
  /**
   * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
   */
  created() {
    // 如果登录的话才会调用获取数据的方法
    if (this.user) {
      this.users();
    }
  },
  /**
   * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
   * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
   */
  mounted() {},
};
</script>

<style scoped lang="less">
// 头部样式
.header {
  height: 361px;
  background: url("~@/assets/banner.png");
  background-size: cover;
}

// 未登录样式
.not-login {
  display: flex;
  justify-content: center;
  align-items: center;
  .ce {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    img {
      width: 132px;
      height: 132px;
      margin-bottom: 15px;
    }
    span {
      font-size: 28px;
      color: #fff;
    }
  }
}

// 登录样式
.yes-login {
  .top {
    height: 231px;
    padding: 76px 22px 23px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    box-sizing: border-box;
    .left {
      display: flex;
      align-items: center;
      .tu {
        width: 132px;
        height: 132px;
        margin-right: 23px;
        border: 5px solid #fff;
      }
      span {
        font-size: 30px;
        color: #fff;
      }
    }
  }
  .bottom {
    height: 130px;
    display: flex;
    align-items: center;
    .data-item {
      flex: 1;
      display: flex;
      align-items: center;
      flex-direction: column;
      color: #fff;
      .num {
        font-size: 36px;
      }
      .texts {
        font-size: 23px;
      }
    }
  }
}

// 导航样式
.grid-dh {
  margin-bottom: 9px;
  .grid-item {
    height: 141px;
    i {
      font-size: 45px;
    }
    .su-shoucang {
      color: #eb5253;
    }
    .su-lishi {
      color: #ff9d1d;
    }
    span {
      font-size: 28px;
    }
  }
}

// 小智同学距离退出登录的距离
.distance {
  margin-bottom: 9px;
}

//退出登录字体颜色
.exit {
  color: #eb5253;
}
</style>
